#include "execute_task.h"
 
TaskHandle_t exetureHandler;


void showContrl();
void alarmContrl();
void airContrl();
void curtainContrl();


extern TargetGatherData  targetData;
THData thData;
// 执行模块初始化
void init_execute()
{
    if(thData.state==0)
    {
        thData.state = 15; //1111
		thData.tempMax = 200;
		thData.tempMin = -20;
		thData.humiMax = 200;
		thData.humiMin = 20;
		thData.sunshine = 100; 
		thData.smoke = 200;
		
    }
    ledInit();BEEP_Init();OLED_Init();
    
}
// 执行模块任务
void exeture_task(void* parm)
{
    while(1)
    {
        // 判断用户做哪些控制
        if(thData.state&0x01)//显示
        {
            showContrl();
        }
        if(thData.state&0x02)//告警
        {
            alarmContrl();
        }
        if(thData.state&0x04)//空调
        {
            airContrl();
        }
        if(thData.state&0x08)//窗帘
        {
            curtainContrl();
        }
        vTaskDelay(10);
    }
}

void createExecuteTask()
{
    xTaskCreate(exeture_task,"exeture_task",
        128,NULL,2,&exetureHandler);
}


void showContrl()
{   OLED_Clear();
    OLED_ShowString(0,0,  "Temp:  C",12);
	OLED_ShowString(0,15, "Humi:  %",12);
    OLED_Refresh_Gram();
    OLED_ShowNum(5*6,0,targetData.temp,2,12);
	OLED_ShowNum(5*6,15,targetData.humi,2,12); 
    OLED_Refresh_Gram();
}
void alarmContrl()
{
    u8 beepflag=0;
    if(targetData.temp<thData.tempMin||targetData.temp>thData.tempMax)
    {
        LED1=0;beepflag=1;
    }
    else
    {
        LED1=1;
    }
     
    if(targetData.humi<thData.humiMin||targetData.humi>thData.humiMax)
    {
        LED2=0;beepflag=1;
    }
    else
    {
        LED2=1;
    }
    if(targetData.smoke>thData.smoke)
    {
        LED3=0;beepflag=1;
    }
    else
    {
        LED3=1;
    }

    if(targetData.sunshine>thData.sunshine)
    {
        LED4=0;beepflag=1;
    }
    else
    {
        LED4=1;
    }

// 雨
    if(!targetData.raidrop)
    {
        LED5=0;beepflag=1;
    }else{LED5=1;}

    if(!beepflag)
    {
        BEEP=1;//不响
    }
    else{
        while(1)
        {
            BEEP=!BEEP;delay_us(20);
        }
        
    }
    
}
void airContrl()
{

}
void curtainContrl()
{

}